<template>
  <el-dialog
      class="commonDialog"
      v-if="visible"
      :visible.sync="visible"
      :fullscreen="true"
      :custom-class="customClass"
      :modal="false"
      :show-close="false"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :lock-scroll="false"
      :top="top"
      :modal-append-to-body="modalAppendToBody"
      :append-to-body="false"
  >
    <template slot="title">
      <img class="bg1" src="../../assets/img/commonDialog/mdBg1.png" alt="" />
      <img class="bg2" src="../../assets/img/commonDialog/mdBg2.png" alt="" />
      <div class="kpiTitle">{{ kpiTitle }}</div>
      <i class="el-icon-close" style="" @click="closeDialog"></i>

    </template>
    <div>
        <div class="bodyContent">
          <div class="readonlyModel" v-if="readOnly"></div>
          <slot name="content"></slot>
        </div>
    </div>
    <div slot="footer">
      <el-button size="small"  plain type="info" @click="closeDialog" style="border-color:#e4e9ef">取消</el-button>
      <el-button size="small" :loading="loading" type="primary" plain @click="okClick" style="margin-right:20px;border-color: #2d69ed;">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>

import baseComp from "./baseComp";
export default {
  name: "commonDialog",
    extends: baseComp,
  props: {
    kpiTitle: {
      type: String,
      default: () => "",
    },
    dialogFormVisible: {
      type: Boolean,
      default: () => false
    },
    customClass: {
      type: String,
      default: 'special'
    },
    modalAppendToBody: {
      type: Boolean,
      default: () => true
    },
    readOnly: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      top: "0vh"
    };
  },
  mounted(){
    this.loadTheme(this.$options.name)
  },
  watch: {
    dialogFormVisible:{
      handler(newVal,oldVal){
          this.visible = newVal;
          if(newVal){
            this.postMessage(1);
          }else if(!newVal){
            this.postMessage(2);
          }
      },
      deep:true,
      immediate:true
    },
  },
  methods: {
    /**
     * 关闭大弹框，并通知homepage改变大弹框样式，同时改变确认按钮加载等待状态
     */
    closeDialog() {
      this.$confirm('确认是否需要缓存？',{
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(_ => {
        this.visible = false;
        let cd = this.$parent.closeDialog;
        if(cd && typeof cd == 'function'){
          this.$parent.closeDialog("commonDialog")
        }else{
          this.$emit("closeDialog", false);
        }
        this.postMessage(2);
        this.loading = false;
      }).catch(_ => {
        this.visible = false;
        let cd = this.$parent.closeDialog;
        if(cd && typeof cd == 'function'){
          this.$parent.closeDialog("commonDialog")
        }else{
          this.$emit("closeDialog", false);
        }
        this.postMessage(2);
        this.loading = false;
      });
    },
    /**
     * 提交确认事件，并改变按钮加载等待状态
     */
    okClick() {
      this.$emit("okClick", this.postMessage)
    },

    /**
     * 开启加载等待状态
     */
    openLoading(){
      this.loading = true;
      this.$set(this, 'loading', true);
    },
    /**
     * 关闭加载按钮等待状态
     */
    closeLoading(){
      this.loading = false;
    },
    // sendMessage to home
    postMessage(type){
        if(type==1){
            window.parent.postMessage(JSON.stringify({method:'openDialog'}),
            "*")
        }else if(type==2){
              window.parent.postMessage(JSON.stringify({method:'closeDialog'}),
            "*")
        }
    },
  },
};

</script>
<style lang="less" scoped>
/deep/.el-dialog{
  background: #F5F5F5;
}
/deep/.el-dialog__footer{
  padding: 9px 0px !important;
  box-shadow: 0 2px 12px 0 #e8ebf6;
  background: #FFFFFF;
  position: fixed;
  width: 100%;
}
/deep/.el-dialog__header{
  padding: 0px;
  height: 50px;
  line-height: 50px;
  margin: 24px 24px 0px 24px !important;
  background: #FFFFFF;
  position: relative;
  .bg1 {
    position: absolute;
    top: -20px;
    left: -20px;
    z-index: 10;
  }
  .bg2 {
    position: absolute;
    top: -20px;
    left: -20px;
    z-index: 20;
  }
  .el-icon-close{
    cursor: pointer;
    font-size: 24px;
    position: absolute;
    right: 15px;
    top: 12px;
  }
  .kpiTitle {
    color: #28394F;
    font-weight: bold;
    font-size: 16px;
    position: absolute;
    left: 50px;
  }
}
/deep/.el-dialog__body{
   margin: 0px 24px !important;
   padding: 0px 15px 0px 30px !important;
   box-shadow: 0 2px 12px 0 #e8ebf6;
   background: #FFFFFF;
   .bodyContent{
      height: calc(100vh - 50px - 50px - 24px);
      overflow-y: auto;
    }
}
/** 滚动条样式 */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-thumb {
  background: rgb(201, 201, 202);
  border-radius: 4px;
}
::-webkit-scrollbar-track {
  border-radius: 4px;
}
.bodyContent{
  .readonlyModel{
    position: fixed;
    top: 0px;
    z-index: 1994;
    bottom: 50px;
    right: 50px;
    left: 0px;
  }
}
</style>

